package chenji.moreitemfunapi.textures.info;

import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.bukkit.Material;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import chenji.moreitemfunapi.MifAPI;
import chenji.moreitemfunapi.item.MifItem;

public abstract class TextureInfo {
	private static int UID = 0;
	public static final String DEFAULT = "default";
	private static final File root = new File(MifAPI.getMDataFolder() + "/textures");
	protected Set<String> textures;
	protected Set<String> auts;
	private String pluginName;
	private String itemName;
	private Material itemtype;
	private boolean haveDefault;
	private int uid;
	private Map<String, String> defaulttextures = Maps.newHashMap();
	private MifItem mifitem;

	protected TextureInfo(String pluginName, String itemName, Material itemtype) {
		UID++;
		this.uid = UID;
		this.textures = Sets.newHashSet();
		this.textures.add(DEFAULT);
		this.auts = Sets.newHashSet();
		this.pluginName = pluginName;
		this.itemName = itemName;
		this.itemtype = itemtype;
		this.mifitem = MifAPI.getItemManager().getMifItem(this.itemName);
	}

	public boolean loadDefaultTexture(String name) {
		if (this.defaulttextures.containsKey(name)) {
			if (MifAPI.getTexturesManager().downloadItem(this.defaulttextures.get(name), this.getImageName(name))) {
				this.update(name, null);
				return false;
			} else {
				return false;
			}
		}
		return false;
	}

	public void setDefaultTexture(Map<String, String> defaulttextures) {
		this.defaulttextures = defaulttextures;
	}

	public int loadAllNotUpdatedTexture() {
		int amount = 0;
		for (String s : this.getNotUpdatedTextures()) {
			if (!this.loadDefaultTexture(s)) {
				amount++;
			}
		}
		return amount;
	}

	protected TextureInfo(Set<String> textures, String pluginName, String itemName, Material itemtype) {
		this(pluginName, itemName, itemtype);
		this.textures.addAll(textures);
	}

	public abstract Properties getProperties();

	protected final void setHaveDefault(boolean b) {
		this.haveDefault = b;
		if (!b) {
			this.textures.remove(DEFAULT);
		} else {
			this.textures.add(DEFAULT);
		}
	}

	public final int getUID() {
		return this.uid;
	}

	@SuppressWarnings("deprecation")
	protected final Properties getProperties(String type) {
		Properties p = new Properties();
		p.setProperty("type", type);
		p.setProperty("items", Integer.toString(itemtype.getId()));
		for (String s : textures) {
			if ((!haveDefault) && s.equalsIgnoreCase(DEFAULT)) {
				continue;
			}
			String file = this.getImageName(s);
			String key = this.getPropertiesKey(s);
			p.setProperty(key, file);
		}
		p.setProperty("nbt.miftexturesname", this.itemName);
		return p;
	}

	public abstract boolean isDone();

	private String getPropertiesKey(String name) {
		String key = name.equalsIgnoreCase(DEFAULT) ? "" : "." + name;
		return "texture" + key;
	}

	/* 获取图像名称 */
	public String getImageName(String name) {
		if (name.equalsIgnoreCase(DEFAULT)) {
			return this.pluginName + this.itemName;
		} else {
			return this.itemName + name;
		}
	}

	public abstract String getPropertiesFileName();

	public Set<String> getAlreadUpdatedTextures() {
		return this.auts;
	}

	public String getPluginName() {
		return this.pluginName;
	}

	public String getItemName() {
		return this.itemName;
	}

	public void update() {
		for (String s : this.textures) {
			File f = new File(root + "/" + this.getImageName(s));
			if (!f.exists()) {
				this.auts.add(s);
			}
		}
	}

	protected void addTexture(String name) {
		this.textures.add(name);
	}

	public abstract boolean update(String nane, File path);

	public final Set<String> getNotUpdatedTextures() {
		Set<String> all = Sets.newHashSet();
		all.addAll(this.textures);
		all.removeAll(this.auts);
		return all;
	}

	public final int getAlreadUpdatedAmount() {
		return this.getAlreadUpdatedTextures().size();
	}

	public final int getTextureAmount() {
		return this.textures.size();
	}

	protected Material getItemType() {
		return this.itemtype;
	}

	public final Set<String> getTextures() {
		return this.textures;
	}

	public final MifItem getMifItem() {
		return this.mifitem;
	}

	public final boolean contain(String name) {
		if (this.textures.contains(name) && this.getAlreadUpdatedTextures().contains(name)) {
			return true;
		}
		return false;
	}

	public final boolean removePack(String name) {
		if (this.textures.contains(name)) {
			if (this.getAlreadUpdatedTextures().contains(name)) {
				File f = new File(root + "/" + this.getImageName(name));
				if (f.exists()) {
					if (f.delete()) {
						this.auts.remove(name);
						return true;
					}
				}
			}
		}
		return false;
	}

	public void clearAll() {
		for (String name : this.textures) {
			this.removePack(name);
		}
	}

}
